<?php

namespace App\Jobs\Posts;

use App\Wen\Utils\PostUtils;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Redis;

class UpdatePostPopularCommentJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    public $tries = 1;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct()
    {

    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        $set_key = 'post:popular:comment:set';
        $todo_set = Redis::smembers($set_key);
        if($todo_set) {
            foreach ($todo_set as $post_id) {
                if (_empty_($post_id)) {
                    Redis::srem($set_key, $post_id);
                    continue;
                }
                $comment_arr = PostUtils::get_post_popular_comment($post_id);
                if($comment_arr && _array_key($comment_arr, 'id', 0) > 0){
                    Cache::put('post:popular:comment:'.$post_id, json_encode($comment_arr), 1200);
                }
                // todo:
                Redis::srem($set_key, $post_id);
            }
        }
    }


}
